package com.emser.unc.client.view;

import com.emser.unc.client.CloudMobile;
import com.emser.unc.client.presenter.ProductPresenter;
import com.emser.unc.client.resources.GlobalResources;
import com.emser.unc.serializable.ProductDTO;
import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.ImageResourceCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.Range;

public class ProductsView extends Composite implements ProductPresenter.Display{
	private VerticalPanel verticalPanel;
	private Button btnAdd, btnRemove,btnUpdate;
	private CellTable<ProductDTO> productsTable;
	private MultiSelectionModel<ProductDTO> selectionModel;
	private Column<ProductDTO, String> editColumn;
	private SimplePager pager;
	private PushButton btnSearch;
	private TextBox searchParam;
	private final GlobalResources resource;
	private MessageWView messageDelete;
	private BusyIndicatorView busyIndicator = new BusyIndicatorView(CloudMobile.constants.msjWorking());;
	private ProvidesKey<ProductDTO> KEY_PROVIDER = new ProvidesKey<ProductDTO>() {
	      @Override
	      public Object getKey(ProductDTO item) {
	        return item == null ? null : item.getCode();
	      }
	    };
	private Label lastUpdate;
	private int heightSPanel;
	
	public ProductsView() {
		verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setSize("100%", CloudMobile.get().getHeightPanelCenter()+"px");
		heightSPanel = CloudMobile.get().getHeightPanelCenter() - 100;
		resource = GWT.create(GlobalResources.class);
		initHPanelTitle();
		initScrollPanel();		
	    initPager();
	    messageDelete = new MessageWView();
	    messageDelete.getLblMessageW().setText(CloudMobile.constants.msjWarningDelete());
	    messageDelete.hide();
	    busyIndicator.hide();
	}
	
	private void initScrollPanel() {
		ScrollPanel sPanel = new ScrollPanel();
		sPanel.setSize("100%-1px", heightSPanel+"px");
		selectionModel = new MultiSelectionModel<ProductDTO>(KEY_PROVIDER);
		productsTable= new CellTable<ProductDTO>(KEY_PROVIDER);
	    initColumnsTable();
	    productsTable.setSelectionModel(selectionModel, DefaultSelectionEventManager.<ProductDTO>createCheckboxManager());
	    productsTable.setSize("100%", "100%");
	    sPanel.setWidget(productsTable);
	    verticalPanel.add(sPanel);
	    verticalPanel.setCellHorizontalAlignment(sPanel, HasHorizontalAlignment.ALIGN_CENTER);
	    verticalPanel.setCellWidth(sPanel, "100%");
	    verticalPanel.setCellHeight(sPanel, heightSPanel +"px");
	}

	private void initPager() {
		SimplePanel simplePanel = new SimplePanel();
	    verticalPanel.add(simplePanel);
	    verticalPanel.setCellVerticalAlignment(simplePanel, HasVerticalAlignment.ALIGN_MIDDLE);
	    simplePanel.setSize("400px", "40px");
	    verticalPanel.setCellHorizontalAlignment(simplePanel, HasHorizontalAlignment.ALIGN_CENTER);
	    verticalPanel.setCellWidth(simplePanel, "100%");
	    verticalPanel.setCellHeight(simplePanel, "40px");
	    pager = new SimplePager(TextLocation.CENTER){
	    	@Override
	    	public void setPageStart(int index) {
	    	  if (getDisplay() != null) {
	    	    Range range = getDisplay().getVisibleRange();
	    	    int pageSize = range.getLength();
	    	    index = Math.max(0, index);
	    	    if (index != range.getStart()) {
	    	      getDisplay().setVisibleRange(index, pageSize);
	    	    }
	    	  }
	    	}
	    };
	    pager.setStyleName("gwt-pager1");
	    pager.setStylePrimaryName("gwt-pager1");
	    pager.setRangeLimited(true);
	    pager.setDisplay(productsTable);
	    pager.setSize("400px", "15px");
	    pager.setPageSize(CloudMobile.constants.pageSizeCP());
	    pager.setPage(0);
	    simplePanel.add(pager);
	}

	private void initHPanelTitle() {
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setStyleName("gwt-hPanelCenter");
		horizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		verticalPanel.add(horizontalPanel);
		verticalPanel.setCellHeight(horizontalPanel, "40px");
		verticalPanel.setCellWidth(horizontalPanel, "100%");
		horizontalPanel.setSize("100%", "40px");
		
		Grid grid = new Grid(1, 4);
		horizontalPanel.add(grid);
		horizontalPanel.setCellVerticalAlignment(grid, HasVerticalAlignment.ALIGN_MIDDLE);
		
		btnAdd = new Button(CloudMobile.constants.add());
		grid.setWidget(0, 0, btnAdd);
		btnAdd.setSize("112", "28");
		
		btnRemove = new Button(CloudMobile.constants.remove());
		grid.setWidget(0, 1, btnRemove);
		btnRemove.setSize("112", "28");
		btnUpdate = new Button(CloudMobile.constants.update());
	    grid.setWidget(0, 2, btnUpdate);
	    btnUpdate.setSize("112", "28");
	    lastUpdate = new Label("");
	    grid.setWidget(0, 3, lastUpdate);
	    lastUpdate.setSize("200", "20");
	    
		grid.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_LEFT);
	    Grid grid_1 = new Grid(1, 2);
	    horizontalPanel.add(grid_1);
	    horizontalPanel.setCellVerticalAlignment(grid_1, HasVerticalAlignment.ALIGN_MIDDLE);
	    horizontalPanel.setCellHorizontalAlignment(grid_1, HasHorizontalAlignment.ALIGN_RIGHT);
	    searchParam = new TextBox();
	    grid_1.setWidget(0, 0, searchParam);
	    searchParam.setSize("200px", "28");
	    
	    btnSearch = new PushButton(CloudMobile.constants.search());
	    grid_1.setWidget(0, 1, btnSearch);
	    grid_1.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT);
	    grid_1.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_RIGHT);
	}
	private void initColumnsTable(){
		Column<ProductDTO, Boolean> checkColumn = new Column<ProductDTO, Boolean>(
	    	    new CheckboxCell(true, false))
	    	        {
	    	            @Override
	    	            public Boolean getValue(ProductDTO t)
	    	            {
	    	                return selectionModel.isSelected(t);
	    	            }
	    	        };
		checkColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		productsTable.addColumn(checkColumn, "");
		Column<ProductDTO, ImageResource> imageColumn =
		  new Column<ProductDTO, ImageResource>(new ImageResourceCell()) {
		    @Override
		    public ImageResource getValue(ProductDTO object) {
		      return resource.getImageProduct();
		    }
		  };
		imageColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		productsTable.addColumn(imageColumn, "");
		TextColumn<ProductDTO> codeColumn = new TextColumn<ProductDTO>() {
	      @Override
	      public String getValue(ProductDTO object) {
	        return object.getCode().toString();
	      }
	    };
	    codeColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
	    productsTable.addColumn(codeColumn, CloudMobile.constants.code());  
	    TextColumn<ProductDTO> titleColumn = new TextColumn<ProductDTO>() {
	      @Override
	      public String getValue(ProductDTO object) {
	        return object.getTitle();
	      }
	    };
	    titleColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
	    productsTable.addColumn(titleColumn, CloudMobile.constants.title());
	    
	    TextColumn<ProductDTO> unitPriceColumn = new TextColumn<ProductDTO>() {
		      @Override
		      public String getValue(ProductDTO object) {
		        return object.getUnitPrice().toString();
		      }
		    };
		unitPriceColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		productsTable.addColumn(unitPriceColumn, CloudMobile.constants.price());	
		TextColumn<ProductDTO> stockColumn = new TextColumn<ProductDTO>() {
		      @Override
		      public String getValue(ProductDTO object) {
		        return object.getStock().toString();
		      }
		    };
		stockColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		productsTable.addColumn(stockColumn, CloudMobile.constants.stock());	        
	    editColumn = new Column<ProductDTO, String>(new ButtonCell()) {
	    	@Override
	    	public String getValue(ProductDTO object) {
	    		return " Editar";
	    	}
	    };
	    editColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
	    productsTable.addColumn(editColumn, "");	    
	}

	@Override
	public HasClickHandlers getAddButton() {
		return btnAdd;
	}

	@Override
	public HasClickHandlers getRemoveButton() {
		return btnRemove;
	}

	@Override
	public MultiSelectionModel<ProductDTO> getSelectionModel() {
		return selectionModel;
	}

	@Override
	public HasData<ProductDTO> getTable() {
		return productsTable;
	}

	@Override
	public Column<ProductDTO, String> getEditColumn() {
		return editColumn;
	}

	@Override
	public void enableDelete(boolean b) {
		btnRemove.setEnabled(b);	
	}

	@Override
	public HasClickHandlers getSearchButton() {
		return btnSearch;
	}

	@Override
	public HasValue<String> getSearchParam() {
		return searchParam;
	}

	@Override
	public void showWarning() {
		messageDelete.show();
	}

	@Override
	public void disableWarning() {
		messageDelete.hide();
	}

	@Override
	public HasClickHandlers getBtnPopUpDCancel() {
		return messageDelete.getBtnCancel();
	}

	@Override
	public HasClickHandlers getBtnPopUpDDelete() {
		return messageDelete.getBtnDelete();
	}
	@Override
	public void enableBtnPopUpDelete(boolean enabled) {
		messageDelete.getBtnDelete().setEnabled(enabled);	
	}

	@Override
	public void enableBtnSearch(boolean enabled) {
		btnSearch.setEnabled(enabled);	
	}

	@Override
	public void enableBtnAdd(boolean enabled) {
		btnAdd.setEnabled(enabled);	
	}
	@Override
	public void showBusyIndicator(){
		busyIndicator.show();
	}
	@Override
	public void hideBusyIndicator(){
		busyIndicator.hide();
	}

	@Override
	public void setPage(int page) {
		pager.setPage(page);		
	}

	@Override
	public int getPage() {
		return pager.getPage();
	}

	@Override
	public HasClickHandlers getUpdateButton() {
		return btnUpdate;
	}

	@Override
	public void setLastUpdate(String text) {
		lastUpdate.setText(text);
		
	}

	@Override
	public void enableBtnUpdate(boolean enabled) {
		btnUpdate.setEnabled(enabled);
	}
	
}
